from django.contrib.auth.backends import ModelBackend

from .models import Account


class UserBackend(ModelBackend):
    """ 自定义用户认证后端口 """

    def authenticate(self, username=None, password=None, role=1):
        """
        重写认证方法
        :param username:
        :param password:
        :param role:
        :return: user
        """
        account = Account.objects.filter(username=username, role=role).first()
        if account and account.check_password(password):
            return account
        else:
            return None
